//
//  DHVisitorView.swift
//  Weibo
//
//  Created by Holfioo on 15/9/1.
//  Copyright © 2015年 Holfioo. All rights reserved.
//

import UIKit

// 声明代理方法
//protocol VisitorViewDelegate: NSObjectProtocol {
//    func visitorViewWillRegister()
//    func visitorViewWillLogin()
//}

class DHVisitorView: UIView {
    // 代理属性
//    weak var delegate: VisitorViewDelegate?
    
//    @objc private func registerClick() {
////        delegate?.visitorViewWillRegister()
//    }
    
//    @objc private func loginClick() {
////        delegate?.visitorViewWillLogin()
//    }
    
    // 子试图调用的方法，改变标题和图片
    func setupViewWith(title: String, imageNage: String?) {
        messageView.text = title;
        // 如果有图片的话，再赋值
        if let imaName = imageNage {
            homeView.image = UIImage(named: imaName)
            mainCircle.hidden = true
            // 提示技能。
            // bringSubviewToFront(coverView)
            // sendSubviewToBack(coverView)
        }else
        {
            runHorse() // 让图片开始转动
        }
        
    }
    
    // 手码的时候
    override init(frame: CGRect) {
        super.init(frame: frame)
        setSubviewsFrame()
        // runHorse()
    }
    // 用xib 和 storyboard的时候会调用此方能次方。所以为了方便
    required init?(coder aDecoder: NSCoder) {
        //        fatalError("init(coder:) has not been implemented")
        super.init(coder: aDecoder)
        setSubviewsFrame()
    }
    
    // 给mainCircle 动画效果
    private func runHorse() {
        let ani = CABasicAnimation(keyPath: "transform.rotation")
        ani.duration = 10
        ani.repeatCount = MAXFLOAT
        ani.toValue = 2 * M_PI
        /// 设置动画属性不会停止, 并不是一直在，在mainCircle消失的时候 一起消失
        ani.removedOnCompletion = false
        // 增加得到中间的圈上
        mainCircle.layer.addAnimation(ani, forKey: nil)
    }
    
    private func setSubviewsFrame() {
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1)
        /// 增加子试图
        addSubview(mainCircle)
        addSubview(coverView)
        addSubview(homeView)
        addSubview(messageView)
        addSubview(registerBtn)
        addSubview(loginBtn)
        // 重要的事情要三遍，NSAutoresizingMaskLayoutConstraints 要开启，要开启，要开启
        mainCircle.translatesAutoresizingMaskIntoConstraints = false
        // 布局子试图
        addConstraint(NSLayoutConstraint(item: mainCircle, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: mainCircle, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -50))
        // 房子
        homeView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: mainCircle, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: mainCircle, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        // 文字信息
        messageView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: messageView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: mainCircle, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 20))
        addConstraint(NSLayoutConstraint(item: messageView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: mainCircle, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: messageView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 224))
        // 注册按钮
        registerBtn.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 20))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: messageView, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        // 登录按钮
        loginBtn.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 20))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: messageView, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        // 设置cover的属性 用VFL语言
        coverView.translatesAutoresizingMaskIntoConstraints = false
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[v]-0-|", options: [], metrics: nil, views: ["v" : coverView]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[v]-(-35)-[regBtn]", options: [], metrics: nil, views: ["v" : coverView, "regBtn" : registerBtn]))
    }
    
    //MARK: - 懒加载控件
    // 圆环
    private lazy var mainCircle: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    // 小房子
    private lazy var homeView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    // 描述文字
    private lazy var messageView: UILabel = {
        let message = UILabel()
        message.text = "关注一些人，回这里看看有什么"
        message.font = UIFont.systemFontOfSize(14)
        message.textColor = UIColor.darkGrayColor()
        message.textAlignment = NSTextAlignment.Center
        message.numberOfLines = 0 // 换行
        return message
    }()
    // 不要设置成私有的。供全局访问
    // 注册按钮
    lazy var registerBtn: UIButton = {
        let btn = UIButton(type: UIButtonType.Custom)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitle("注册", forState: UIControlState.Normal)
        //        btn.setAttributedTitle(title: NSAttributedString?, forState: <#T##UIControlState#>)
//        btn.addTarget(self, action: "registerClick", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
    // 登录按钮
    lazy var loginBtn: UIButton = {
        let btn = UIButton(type: UIButtonType.Custom)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitle("登录", forState: UIControlState.Normal)
//        btn.addTarget(self, action: "loginClick", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
        }()
    // 遮罩层
    private lazy var coverView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
}
